import { number } from "echarts";

/**
 * @param {string} path
 * @returns {Boolean}
 */
export function isExternal(path) {
  return /^(https?:|mailto:|tel:)/.test(path);
}

/**
 * @param {string} str
 * @returns {Boolean}
 */
export function validUsername(str) {
  const valid_map = ["admin", "editor"];
  return valid_map.indexOf(str.trim()) >= 0;
}

/**
 * @param {string} url
 * @returns {Boolean}
 */
export function validURL(url) {
  const reg =
    /^(https?|ftp):\/\/([a-zA-Z0-9.-]+(:[a-zA-Z0-9.&%$-]+)*@)*((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[1-9][0-9]?)(\.(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[1-9]?[0-9])){3}|([a-zA-Z0-9-]+\.)*[a-zA-Z0-9-]+\.(com|edu|gov|int|mil|net|org|biz|arpa|info|name|pro|aero|coop|museum|[a-zA-Z]{2}))(:[0-9]+)*(\/($|[a-zA-Z0-9.,?'\\+&%$#=~_-]+))*$/;
  return reg.test(url);
}

/**
 * @param {string} str
 * @returns {Boolean}
 */
export function validLowerCase(str) {
  const reg = /^[a-z]+$/;
  return reg.test(str);
}

/**
 * @param {string} str
 * @returns {Boolean}
 */
export function validUpperCase(str) {
  const reg = /^[A-Z]+$/;
  return reg.test(str);
}

/**
 * @param {string} str
 * @returns {Boolean}
 */
export function validAlphabets(str) {
  const reg = /^[A-Za-z]+$/;
  return reg.test(str);
}

/**
 * @param {string} email
 * @returns {Boolean}
 */
export function validEmail(email) {
  const reg =
    /^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
  return reg.test(email);
}

/**
 * @param {string} str
 * @returns {Boolean}
 */
export function isString(str) {
  if (typeof str === "string" || str instanceof String) {
    return true;
  }
  return false;
}

/**
 * @param {Array} arg
 * @returns {Boolean}
 */
export function isArray(arg) {
  if (typeof Array.isArray === "undefined") {
    return Object.prototype.toString.call(arg) === "[object Array]";
  }
  return Array.isArray(arg);
}

/**
 * 名称的校验
 * @param {String} name 汉字描述
 * @returns
 */
export function nameValidate(name, length = 32) {
  return (rule, value, callback) => {
    // const reg = /^[\w\u4E00-\u9FA5\_\-\.\:\@\#\*\/]+$/;
    const reg = /^[\w\u4E00-\u9FA5\_\-\.:@#*\s/]+$/
    if (value) {
      if (!reg.test(value)) {
        callback(new Error("只能包含汉字、英文、数字、或者（.:_-@#*）"));
      } else if (value.length > length) {
        callback(new Error(`长度不能超过${length}字`));
      } else {
        callback();
      }
    } else {
      callback(new Error("请输入" + name + "名称"));
    }
  };
}

/**
 * 链接的校验：只能以http:// 或 https:// 开头
 * @param {String} name 汉字描述
 * @returns
 */
export function linkValidate(name, len) {
  return (rule, value, callback) => {
    console.log("value", value);
    const reg = /(http|https):\/\/\S*/;
    if (value) {
      if (!reg.test(value)) {
        callback(new Error("开头必为为：http://或https://"));
      } else if (value.length > len) {
        callback(new Error("长度不超过" + len + "字"));
      } else {
        callback();
      }
    } else {
      callback(new Error("请输入" + name + "链接"));
    }
  };
}

/**
 * 当前年月日的获取
 * @param
 * @returns
 */
export function getNowTime() {
  let date = new Date();
  let year = date.getFullYear();
  let month = date.getMonth() + 1;
  let day = date.getDate();
  let hours = date.getHours();
  let minute =
    date.getMinutes() * 1 < 10 ? "0" + date.getMinutes() : date.getMinutes();
  let seconds =
    date.getSeconds() * 1 < 10 ? "0" + date.getSeconds() : date.getSeconds();
  let res = {
    year,
    month,
    day,
    hours,
    minute,
    seconds,
  };
  return res;
}

/**
 * 输入框只保留数字
 * @param {String} val 值
 * @returns
 */
export function onlyNumber(val) {
  let num = val.replace(/\D/g, "");
  return num;
}

/**
 * 输入框只保留数字或不限
 * @param {String} val 值
 * @returns
 */
export function regNumber(val) {
  let num = "";
  if (val === "不限") {
    num = val;
  } else {
    num = val.replace(/\D/g, "");
  }
  return num;
}

/**
 * 判断身份证号是否合法
 * @param {*} value 身份证号
 * @returns 校验失败返回false，成功为true
 */
export function validateIdentityCode(value) {
  if (!value || value.length !== 18) return false;
  let reg =
    /^\d{6}(18|19|([23]\d))\d{2}((0[1-9])|(10|11|12))(([0-2][1-9])|10|20|30|31)\d{3}[0-9Xx]$/;
  if (!reg.test(value)) return false;

  const wiMap = {
    2: 2,
    3: 4,
    4: 8,
    5: 5,
    6: 10,
    7: 9,
    8: 7,
    9: 3,
    10: 6,
    11: 1,
    12: 2,
    13: 4,
    14: 8,
    15: 5,
    16: 10,
    17: 9,
    18: 7,
  };
  const yMap = {
    0: "1",
    1: "0",
    2: "X",
    3: "9",
    4: "8",
    5: "7",
    6: "6",
    7: "5",
    8: "4",
    9: "3",
    10: "2",
  };
  let s = 0;
  for (let i = 2; i <= 18; i++) {
    s += wiMap[i] * value[18 - i];
  }
  return yMap[s % 11] === value.charAt(17).toUpperCase();
}

/**
 * 判断手机号是否合法
 * @param {*} value 手机号
 * @returns 校验失败返回false，成功为true
 */
export function validatePhone(value) {
  return /^1[3-9]\d{9}$/.test(value);
}

/**
 * 判断入参是否全为数字
 * @param {*} value
 * @returns 校验失败返回false，成功为true
 */
export function validateNum(value) {
  return /^[0-9]*$/.test(value);
}

/**
   * @Description: 格式化日期
   * date:需要格式化的日期Date()类型
   * fmt：日期格式，例如：yyyy-MM-dd、yyyy-MM-dd hh:mm:ss
   * @param  {[Date,str]} y [description]
   * @return {[type]}   [description]
   */
export function formatDate(date, fmt) {
  if(typeof date === 'string') date = new Date(date)
  if (/(y+)/.test(fmt)) fmt = fmt.replace(RegExp.$1, (date.getFullYear() + '').substr(4 - RegExp.$1.length))
  let o = {
    'M+': date.getMonth() + 1,
    'd+': date.getDate(),
    'h+': date.getHours(),
    'm+': date.getMinutes(),
    's+': date.getSeconds()
  }
  for (let k in o) {
    if (new RegExp(`(${k})`).test(fmt)) {
      let str = o[k] + ''
      fmt = fmt.replace(RegExp.$1, RegExp.$1.length === 1 ? str : ('00' + str).substr(str.length))
    }
  }
  return fmt
}
